import 'package:archive/archive_io.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_log.dart';

/*
解压缩:
Zip (Archive)
Tar (Archive)
ZLib [Inflate decompression]
GZip [Inflate decompression]
BZip2 [decompression]
XZ [decompression]

https://pub-web.flutter-io.cn/packages/archive


dependencies:
  archive: ^3.3.7

import 'package:archive/archive.dart';

 */
abstract class XCompressUtils{

  /*
  Zip解压
  方法1:
  import 'package:archive/archive_io.dart';
// ...
  // Use an InputFileStream to access the zip file without storing it in memory.
  final inputStream = InputFileStream('test.zip');
  // Decode the zip from the InputFileStream. The archive will have the contents of the
  // zip, without having stored the data in memory.
  final archive = ZipDecoder().decodeBuffer(inputStream);
  // For all of the entries in the archive
  for (var file in archive.files) {
    // If it's a file and not a directory
    if (file.isFile) {
      // Write the file content to a directory called 'out'.
      // In practice, you should make sure file.name doesn't include '..' paths
      // that would put it outside of the extraction directory.
      // An OutputFileStream will write the data to disk.
      final outputStream = OutputFileStream('out/${file.name}');
      // The writeContent method will decompress the file content directly to disk without
      // storing the decompressed data in memory.
      file.writeContent(outputStream);
      // Make sure to close the output stream so the File is closed.
      outputStream.close();
    }
  }

  方法2:
  import 'package:archive/archive_io.dart';
// ...
extractFileToDisk('test.zip', 'out');

方法3:
import 'package:archive/archive_io.dart';
// ...
// Use an InputFileStream to access the zip file without storing it in memory.
final inputStream = InputFileStream('test.zip');
// Decode the zip from the InputFileStream. The archive will have the contents of the
// zip, without having stored the data in memory.
final archive = ZipDecoder().decodeBuffer(inputStream);
extractArchiveToDisk(archive, 'out');


   */


  //GZIP解压
  static bool uncompressOfGZip(
      {
        required String zipFilePath,
        required String outFilePath,
      }
      ){

    try{

      InputFileStream inputStream = InputFileStream(zipFilePath);
      List<int> bytes = GZipDecoder().decodeBuffer(inputStream);
      OutputFileStream outputStream = OutputFileStream(outFilePath);
      outputStream.writeBytes(bytes);
      outputStream.close();
      inputStream.close();

      return true;

    }catch(e){
      XLogUtils.printLog('---解压出错:$e');
    }

    return false;
  }



}
